package com.wall.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wall.springbootinit.common.ErrorCode;
import com.wall.springbootinit.exception.BusinessException;
import com.wall.springbootinit.model.dto.menu.MenuDto;
import com.wall.springbootinit.model.dto.menu.MenuUpdateDto;
import com.wall.springbootinit.model.entity.File;
import com.wall.springbootinit.model.entity.Menu;
import com.wall.springbootinit.model.entity.MenuContent;
import com.wall.springbootinit.model.vo.MenuTreeVo;
import com.wall.springbootinit.service.FileService;
import com.wall.springbootinit.service.MenuContentService;
import com.wall.springbootinit.service.MenuService;
import com.wall.springbootinit.mapper.MenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 33932
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{


    @Resource
    private MenuContentService menuContentService;

    @Resource
    private FileService fileService;

    /**
     * 添加菜单
     * @param menu 菜单
     * @return 结果
     */
    @Override
    public boolean addMenu(MenuDto menu) {
        Long pid = menu.getPid();
        String menuName = menu.getMenuName();
        if (StringUtils.isAnyBlank(menuName) || pid == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"目录名不能为空");
        }
        Menu one = lambdaQuery().eq(Menu::getBusinessId, pid).one();
        if (one == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu menu1 = new Menu();
        menu1.setPid(pid);
        menu1.setMenuName(menuName);
        boolean save = save(menu1);
        if (!save){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }

    /**
     * 更新菜单
     * @param menu 菜单
     * @return 结果
     */
    @Override
    public boolean updateMenu(MenuUpdateDto menu) {
        Long businessId = menu.getBusinessId();
        if (businessId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu one = lambdaQuery().eq(Menu::getBusinessId, businessId).one();
        if (one == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        one.setMenuName(menu.getMenuName());
        one.setPid(menu.getPid());
        boolean update = updateById(one);
        if (!update){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }

    /**
     * 获取菜单树
     * @return 菜单树
     */
    @Override
    public MenuTreeVo getTree() {
        Menu menu = lambdaQuery().isNull(Menu::getPid).one();
        // 获取一级菜单
        List<Menu> menuList = lambdaQuery()
                .eq(Menu::getPid, menu.getBusinessId())
                .list();
        // 循环获取菜单
        List<MenuTreeVo> menuTreeVos = menuList.stream().map(menuOne -> {
            MenuTreeVo menuTreeVo = new MenuTreeVo();
            BeanUtils.copyProperties(menuOne, menuTreeVo);
            menuTreeVo.setChildren(getChileren(menuOne.getBusinessId()));
            return menuTreeVo;
        }).collect(Collectors.toList());
        // 获取二级菜单
        MenuTreeVo menuTreeVo = new MenuTreeVo();
        BeanUtils.copyProperties(menu, menuTreeVo);
        menuTreeVo.setChildren(menuTreeVos);
        return menuTreeVo;
    }

    /**
     * 获取菜单图片
     * @param id id
     * @return 结果
     */
    @Override
    public List<File> getMenuImageList(Long id) {
        List<MenuContent> list = menuContentService.list(new LambdaQueryWrapper<MenuContent>()
                .eq(MenuContent::getMenuId, id));
        return list.stream().map(menuContent -> fileService.getById(menuContent.getContent())).collect(Collectors.toList());
    }

    /**
     * 获取菜单
     * @param businessId id
     * @return 结果
     */
    private List<MenuTreeVo> getChileren(Long businessId) {
        List<Menu> menuList = lambdaQuery().eq(Menu::getPid, businessId).list();
        return menuList.stream().map(menuOne -> {
            MenuTreeVo menuTreeVo = new MenuTreeVo();
            BeanUtils.copyProperties(menuOne, menuTreeVo);
            menuTreeVo.setChildren(getChileren(menuOne.getBusinessId()));
            return menuTreeVo;
        }).collect(Collectors.toList());
    }
}




